close all;
% [val text raw] = xlsread('VNIBOR.xlsx',10);
ncol = size(val,2);
%%
rng(1);
Standardize = true;
BoxConstraint = 1;
global gamma;
global intercept;

gamma = 1;intercept = -1;
col = [4 5 7];


Y = val(:,2);
X = val(:,col);

N = size(val,1);
Ntrain = floor(N.*3./4);
Ntest = size(val,1) - Ntrain;

Xtrain = X(1:Ntrain,:);
Ytrain = Y(1:Ntrain);
% dates = datenum(text(2:end,1));

c = cvpartition(Ntrain,'KFold',10);
kfoldLoss(fitcsvm(Xtrain,Ytrain,'CVPartition',c,...
    'KernelFunction','mysigmoid','Standardize',Standardize,'BoxConstraint',BoxConstraint))

%%
% mysigmoid2 = @(z) myfunc(z,c,Standardize,Xtrain, Ytrain);
rng(1);
c = cvpartition(Ntrain,'KFold',10);
opts = optimset('TolX',5e-4,'TolFun',5e-4);

% loose grid
xmin = 5;xmax = 8; %C
ymin = -5; ymax = -2; % alpha
zmin = -1; zmax = -1; %intercept
dx = 1/2; dy = 1/2; dz = 1;

% fine grid
% xmin = -1;xmax = 0;
% ymin = -1; ymax = 0;
% dx = 0.2; dy = 0.2;

nx = (xmax - xmin)/dx +1;
ny = (ymax - ymin)/dy +1;
nz = (zmax - zmin)/dz +1;
fres = zeros(nx,ny,nz);


minValue = 1000;
iMin = [0 0 0];
index1 = 1;
for i = xmin: dx: xmax
    index2 = 1;
    C = 2.^i;
    for  j = ymin: dy : ymax
        index3 = 1;
        gamma = 2.^j;
        for kk = zmin : dz : zmax
            intercept = kk;
            z = [C gamma intercept];
            %fres(index1,index2, index3) = mysigmoid2(z);
            V = kfoldLoss(fitcsvm(Xtrain,Ytrain,...
                'CVPartition',c,'KernelFunction','mysigmoid',...
                'BoxConstraint',z(1),'Standardize',Standardize));
            if(V<minValue)
                minValue = V;
                iMin = [index1 index2 index3];
            end
            fres(index1,index2, index3) = V;
            index3 = index3+1;
        end        
%         fres(index1,index2)= kfoldLoss(fitcsvm(Xtrain,Ytrain,'CVPartition',c,...
%     'KernelFunction','rbf','BoxConstraint',C,...
%     'KernelScale',scale,'Standardize',Standardize));
        index2 = index2+1;
    end
    index1 = index1+1;    
end

% fres;
minV = min(min(min(fres)));

% [ix iy iz] = find(fres == minV)
% z1 = 2.^[(dx*(ix-1)+xmin) (dy*(iy-1)+ymin) (dz*(iz-1)+ymin)]
minV
iMin

z1 = [2.^(dx*(iMin(1)-1)+xmin) 2.^(dy*(iMin(2)-1)+ymin) (dz*(iMin(3)-1)+zmin)]

close all;
contour(xmin:dx:xmax, ymin:dy:ymax,fres','showtext','on')

%%
% BoxContraint = z1(1);
% gamma = z1(2);
% intercept = z1(3);
rng(1)
BoxContraint = 32;
gamma = 1/4;
intercept = -1;

window = Ntest;
Xtrain2 = Xtrain;
Ytrain2 = Ytrain;
res = zeros(Ntest,3);

for i = 1: Ntest   
%     SVMModel = fitcsvm(Xtrain2,Ytrain2,'KernelFunction','rbf','BoxConstraint'...
%         ,BoxConstraint,'KernelScale',KernelScale,'Standardize',Standardize);
    SVMModel = fitcsvm(Xtrain2,Ytrain2,'KernelFunction','mysigmoid',...
        'Standardize',Standardize,'BoxConstraint',BoxConstraint);
    
    % 1. get predict result
    Xtest = X(Ntrain+i,1:end); %no window    
    Ytest = Y(Ntrain+i);    
    [label ,scores] = predict(SVMModel,Xtest);
    res(i,:) = [label Ytest scores(1)];
    
    % 2. update     
    %no window
    Xtrain2 = X(1:Ntrain+i,1:end);
    Ytrain2 = Y(1:Ntrain+i,:);    
    %window    
%     Xtrain2 = X(i:Ntrain+i,1:end);
%     Ytrain2 = Y(i:Ntrain+i,:);    
end


p11 = sum(res(:,1).*res(:,2))./ sum(res(:,2));
p00 = sum((~res(:,1)).*(~res(:,2)))./ sum(~res(:,2));

[sum(res(:,1)==res(:,2))./Ntest p11 p00 (p11+p00)/2]

rng(1);
kfoldLoss(fitcsvm(Xtrain,Ytrain,'CVPartition',c,...
    'KernelFunction','mysigmoid','Standardize',Standardize,'BoxConstraint',BoxConstraint))
%%

SVMModel = fitcsvm(Xtrain,Ytrain,'KernelFunction','mysigmoid',...
        'Standardize',Standardize,'BoxConstraint',BoxConstraint);
[label1 ,scores1] = predict(SVMModel,X(Ntrain+1:end,:));    

res = [Y(Ntrain+1:end) label1];

p11 = sum(res(:,1).*res(:,2))./ sum(res(:,2));
p00 = sum((~res(:,1)).*(~res(:,2)))./ sum(~res(:,2));

[sum(res(:,1)==res(:,2))./Ntest p11 p00 (p11+p00)/2 sum(~res(:,2))./Ntest]

